<script setup>
import { getCityListApi } from '@/api/pcd';
import { addWarehouseApi, updateWarehouseApi } from '@/api/warehouse';
import { ElMessage } from 'element-plus';
import { getCurrentInstance, onMounted, ref } from 'vue';


//获取当前Vue实例
const instance = getCurrentInstance()
const ruleFormRef = ref(null)
const dialogFormVisible = ref(false)
const title = ref('')
const cityList = ref([])
const warehouseForm = ref({
    id: '',
    name: '',
    cityId: '',
    address: '',
    location: '',
})

const reset = () => {
    warehouseForm.value = {
        id: '',
        name: '',
        cityId: '',
        provinceId: '',
        address: '',
        location: '',
    }
    //  isPcdDisabled.value = false // 重置禁用状态
}

const save =async () => {
     if (!ruleFormRef) return
    await ruleFormRef.value.validate(async (valid, fields) => {
        if (valid) {
            let res = null
            if (warehouseForm.value.id) {
                res = await updateWarehouseApi(warehouseForm.value)
            }else {
                res = await addWarehouseApi(warehouseForm.value)
            }
            if (res.code == 1) {
                ElMessage.success('操作成功')
                dialogFormVisible.value = false
                instance.emit('refresh')
            }else{
                ElMessage.error('操作失败')
            }
        } else {
        console.log('error submit!', fields)
        }
    })
}


const rules = ref({
  name: [
    { required: true, message: '名字不能为空', trigger: 'blur' }
  ],
})


const getCityList = async ()=>{
  const result = await getCityListApi()
  cityList.value = result.data
}

defineExpose({
    dialogFormVisible,
    title,
    warehouseForm,
    reset,
    // isPcdDisabled // 暴露这个属性
})

onMounted(()=>{
  getCityList()
})
</script>

<template>
    <el-dialog style="width: 400px;" v-model="dialogFormVisible" :title="title">
    <el-form :model="warehouseForm" :rules="rules" ref="ruleFormRef" label-width="auto">
      <el-form-item label="仓库名字" placeholder="请输入" prop="name" >
        <el-input v-model="warehouseForm.name" />
      </el-form-item>
      <el-form-item label="所在城市" placeholder="请输入" >
        <el-select v-model="warehouseForm.cityId"  placeholder="请选择">
          <el-option
            v-for="item in cityList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" placeholder="请输入" >
        <el-input v-model="warehouseForm.address" />
      </el-form-item>
      <el-form-item label="经纬度" placeholder="请输入" >
        <el-input v-model="warehouseForm.location" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>